package mars.mp3player.service;

import java.io.IOException;

import mars.mp3player.R;
import mars.mp3player.download.HttpDownloader;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class DownloadService extends Service {
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
//		Mp3Bean mp3Bean=(Mp3Bean)intent.getSerializableExtra("mp3Bean");
//		Log.i("info", mp3Bean.toString());
		String fileName=intent.getStringExtra("fileName");
		DownloadThread downloadThread=new DownloadThread(fileName);
		Thread thread=new Thread(downloadThread);
		thread.start();
		return super.onStartCommand(intent, flags, startId);
	}

	class DownloadThread implements Runnable {
		private String url=AppConstant.URL.BASE_URL;
//		private Mp3Bean mp3Bean;
		private String fileName;
		
		public DownloadThread(String fileName){
			this.fileName=fileName;
		}
		@Override
		public void run() {
			String urlString=url+fileName;
			HttpDownloader downloader=new HttpDownloader();
			try {
				int flag=downloader.downloadFile("mp3", fileName, urlString);
				if(flag==AppConstant.DownloadVars.SUCCESS){
					downloadNotify(AppConstant.DownloadVars.SUCCESS, "下载成功", fileName);
					if(fileName.endsWith(".lrc")){
						Intent intent=new Intent(AppConstant.PlayerMsg.ACTION);
						intent.putExtra("lrcName", fileName);
						DownloadService.this.sendBroadcast(intent);
					}
				}else if(flag==AppConstant.DownloadVars.SUCCESSED){
					downloadNotify(AppConstant.DownloadVars.SUCCESSED, "文件已存在", fileName);
				}else if(flag==AppConstant.DownloadVars.FILE_FAILED){
					downloadNotify(AppConstant.DownloadVars.FILE_FAILED, "文件创建失败", fileName);
				}else if(flag==AppConstant.DownloadVars.NO_FILE){
					downloadNotify(AppConstant.DownloadVars.NO_FILE, "服务器中无此文件", fileName);
					if(fileName.endsWith(".lrc")){
						Intent intent=new Intent(AppConstant.PlayerMsg.ACTION);
						intent.putExtra("lrcName", fileName);
						intent.putExtra("failed", 1);
						DownloadService.this.sendBroadcast(intent);
					}
				}
			} catch (IOException e) {
				if(fileName.endsWith(".lrc")){
					Intent intent=new Intent(AppConstant.PlayerMsg.ACTION);
					intent.putExtra("lrcName", fileName);
					intent.putExtra("failed", 1);
					DownloadService.this.sendBroadcast(intent);
				}
				downloadNotify(AppConstant.DownloadVars.FAILED, "下载失败", fileName);
				Log.e("exception", e.getMessage());
			}
		}
		
		public void downloadNotify(int id, String contentText, String fileName){
			String ns = Context.NOTIFICATION_SERVICE;
			NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
			int icon = R.drawable.download_notice;        // icon from resources
			CharSequence tickerText = AppConstant.DownloadVars.NOTIFY;              // ticker-text
			long when = System.currentTimeMillis();
			Notification notification = new Notification(icon, tickerText, when);// notification time
			
			Context context = getApplicationContext();      // application Context
			CharSequence contentTitle = fileName;
			Intent notificationIntent = new Intent(DownloadService.this, DownloadService.class);
			PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, notificationIntent, 0);
			
			notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
			mNotificationManager.notify(id, notification);
		}
		
	}
	
}
